package com.tianqu.tms.www.car.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.www.car.actionform.CarSeniorSearchInputActionForm;
import com.tianqu.tms.www.car.controller.CarSearchInoutResultController;
import com.tianqu.tms.www.car.viewhelper.CarSeniorSearchInputViewhelper;

/**
 * @author zhangxueliang
 * 高级搜索 - 进出库查询 结果
 */
public class CarSearchInoutResultAction extends TmsAction{
	private Logger logger = Logger
	.getLogger(CarSearchInoutResultAction.class);
	public final static String PAGENO = "pageNo";
	public final static String SORTKEY = "sortKey";
	public final static String MAXINDEX = "maxIndex";
	public final static String ASC = "asc";
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		CarSeniorSearchInputActionForm seniorFrom = (CarSeniorSearchInputActionForm)form;
		logger.debug(this.getClass().getName() + "#execute was called.");
		CarSeniorSearchInputViewhelper view = new CarSeniorSearchInputViewhelper();
		if(request.getParameter(PAGENO) == null) {
			uc.setAttribute(PAGENO, view.getPageNo());
			uc.setAttribute(SORTKEY, "7");
			uc.setAttribute(MAXINDEX, "50");
			uc.setAttribute(ASC, "false");
		} else {
			uc.setAttribute(PAGENO, request.getParameter(PAGENO));
			uc.setAttribute(SORTKEY, request.getParameter(SORTKEY));
			uc.setAttribute(MAXINDEX, request.getParameter(MAXINDEX));
			uc.setAttribute(ASC, request.getParameter(ASC));
		}
		CarSeniorSearchInputActionForm seriorForm = (CarSeniorSearchInputActionForm) uc.getInputForm();
		
		if (uc.getAttribute("seniorFrom") != null){
			seriorForm = (CarSeniorSearchInputActionForm) uc.getAttribute("seniorFrom");			
		}
		if(StringUtil.isNullOrBlank(seriorForm.getSerial()) && StringUtil.isNullOrBlank(seriorForm.getThreadStatus())
				&& StringUtil.isNullOrBlank(seriorForm.getGateId()) && StringUtil.isNullOrBlank(seriorForm.getStartTime()) 
				&& StringUtil.isNullOrBlank(seriorForm.getEndTime())){					
			setErrorsToRequest(request, TmsConstants.U0040 ,null);	//必须输入一个查询条件。	
			return mapping.getInputForward();						
		}
		//进出开始日期/区间check
		if ((!StringUtil.isNullOrBlank(seriorForm.getStartTime()))&&(!StringUtil.isNullOrBlank(seriorForm.getEndTime()))) {	
			SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
			//进出开始日期前段
			Date startDate = formatDate.parse(seriorForm.getStartTime());
			//进出开始日期后段				
			Date endDate = formatDate.parse(seriorForm.getEndTime());
			// 比较
			if (endDate.compareTo(startDate) < 0) {
				String[] param = {"进出开始日期/区间"};
				setErrorsToRequest(request, TmsConstants.U0038, param); //进出开始日期/区间填写的后一个日期不能比前一个日期小。
				return mapping.getInputForward();
			}
		}
		
		CarSearchInoutResultController controller = new CarSearchInoutResultController();
		controller.execute(uc);
		List list = (List)uc.getAttribute(TmsConstants.VIEWHELP);
		if(list.size() == 0){
			setErrorsToRequest(request, TmsConstants.U0039 ,null);	//查询的结果不存在。
			return mapping.getInputForward();
		}else if(list.size() > 300){
			setErrorsToRequest(request, TmsConstants.U0041 ,null);	//查询的结果已超过300条记录，请缩小查询范围后再进行查询。
			return mapping.getInputForward();
		}
		view.setCarInoutList(list);
		Car car = (Car)uc.getAttribute(CarSearchInoutResultController.PAGING);
		// viewhelp中设置翻页。
		view.setPageNo(Integer.toString(car.getPageNo()));
		view.setMaxIndex(Integer.toString(car.getMaxIndex()));
		view.setAsc(Boolean.toString(car.isAsc()));
		view.setSortKey(Integer.toString(car.getSortKey()));
		view.setTotalCount(Integer.toString(car.getTotalCount()));
		view.setDateFrom(Integer.toString(car.getDateFrom()));
		view.setDateTo(Integer.toString(car.getDateTo()));
		request.setAttribute(TmsConstants.VIEWHELP, view);//直接把list打到前台
		
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}
}
